package com.mysiteforme.admin.controller.scrap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import com.xiaoleilu.hutool.date.DateUtil;
import com.mysiteforme.admin.annotation.SysLog;
import com.mysiteforme.admin.base.BaseController;
import java.util.Date;
import java.util.HashMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import java.util.Map;

import javax.servlet.ServletRequest;
/**
 * 报废图形
 */
@Controller
@RequestMapping("/admin/scrapChart")
public class ScrapChartController extends BaseController {
	private static final Logger LOGGER = LoggerFactory.getLogger(ScrapChartController.class);

	@RequiresPermissions("scrap:echart")
	@GetMapping("echart")
	@SysLog("跳转新增报废数据页面")
	public String echart() {
		return "/admin/scrap/echart";
	}

	@PostMapping("/echart")
	@ResponseBody
	@SysLog("图表页面请求报废数据")
	public Map<String, Object> chart() {
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("DailyScrap", scrapService.DailyScrap());
		ret.put("FPCScrap", scrapService.FPCScrap());
		ret.put("ProcessScrap", scrapService.ProcessScrap());
		ret.put("YearScrap", scrapService.YearScrap());
		ret.put("type", "success");
		return ret;
	}
	

	@GetMapping("analysis")
	@SysLog("跳转报废分析")
	public String analysis(ModelMap modelMap,Model model) {
		model.addAttribute("ThereScrap",scrapService.DeatailTScrap());
		return "/admin/scrap/analysis";
	}
	
	
	@PostMapping("/analysis")
	@ResponseBody
	@SysLog("报废分析请求数据")
	public Map<String, Object> chartAnalysis(){
		 Map<String, Object> ret = new HashMap<String, Object>();
		 ret.put("DeatialScrap",scrapService.DeatailScrap());	
		 ret.put("NumTenScrap",scrapService.NumTenScrap("Impact/Dent"));	
		 ret.put("ThereScrap",scrapService.DeatailTScrap());	
		 ret.put("type", "success");
		 return ret;
	}
    
	@PostMapping("analysisUpdate")
	@ResponseBody
	@SysLog("报废分析图表数据")
	public Map<String, Object> chartUpdate(String scrapProject){
		 Map<String, Object> ret = new HashMap<String, Object>();
		 ret.put("NumTenScrap",scrapService.NumTenScrap(scrapProject));	
		 ret.put("type", "success");
		 return ret;
	}
	
	
	@GetMapping("byProcess")
	@SysLog("跳转报废分析")
	public String byProcess(ModelMap modelMap,Model model) {
		return "/admin/scrap/byProcess";
	}
	@PostMapping("byProcess")
	@ResponseBody
	@SysLog("每工序报废")
	public Map<String, Object> byProcess(ServletRequest request){
		 Map<String, Object> ret = new HashMap<String, Object>();
         Map map = WebUtils.getParametersStartingWith(request, "s_");
         Map<Object, Object> queryMap = new HashMap<Object, Object>();
         String beginScrapDate = (String) map.get("beginScrapDate");
         String endScrapDate = (String) map.get("endScrapDate");
         if(StringUtils.isNotBlank(beginScrapDate)) {
             Date begin = DateUtil.parse(beginScrapDate);
        	   queryMap.put("start_time",begin );
         }else{
             Date begin = DateUtil.parse(dateWeek.monday());
        	   queryMap.put("start_time",begin );
         }
         if(StringUtils.isNotBlank(endScrapDate)) {
             Date end = DateUtil.parse(endScrapDate);
        	   queryMap.put("end_time",end );
         }else{
             Date end = DateUtil.parse(dateWeek.sunday());
        	   queryMap.put("end_time",end );
         }
         ret.put("byProcess",scrapService.byProcessScrapIndex(queryMap));
		 ret.put("type", "success");
		 return ret;
	}
	
}